import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';

import '../../commont/button.dart';
import '../../commont/label.dart';
import '../../model/model.dart';
import '../../tool/assets.dart';

class EqSwipeItem extends StatefulWidget {
  SignModel signModel;
  bool enable;
  void Function(String) onChange;
  void Function() onChangeForSend;
  EqSwipeItem(
      {super.key,
      required this.signModel,
      required this.enable,
      required this.onChange,
      required this.onChangeForSend});

  @override
  State<EqSwipeItem> createState() => _EqSwipeItemState();
}

class _EqSwipeItemState extends State<EqSwipeItem> {
  bool get enable {
    return widget.enable && widget.signModel.enable;
  }

  Widget _img_item(bool isAdd) {
    return SizedBox(
      width: 18,
      height: 18,
      child: imgButton(
          groundColor: Color_bg_view,
          enable: enable,
          img: Image.asset(isAdd ? assets.add : assets.sum, color: enable ? Colors.black : Color_disEnable),
          onClick: () {
            setState(() {
              widget.signModel.gain = isAdd ? widget.signModel.gain + 0.1 : widget.signModel.gain - 0.1;
              widget.signModel.gain = max(widget.signModel.gain, widget.signModel.min_gain);
              widget.signModel.gain = min(widget.signModel.gain, widget.signModel.max_gain);
            });
            widget.signModel.select = true;
            widget.onChange('EQ');
          }),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        _img_item(true),
        const SizedBox(height: 0),
        Expanded(
            child: kSlider(
          value: widget.signModel.gain,
          min: widget.signModel.min_gain,
          max: widget.signModel.max_gain,
          enable: enable,
          activeColor: Color_theme,
          thumbColor: Color_theme,
          dirction: Axis.horizontal,
          onChanged: (p0) {
            setState(() {
              widget.signModel.select = true;
              widget.signModel.gain = p0.roundDouble(1);
            });
            widget.onChange('');
          },
          onChangedForSend: (p0) => widget.onChangeForSend(),
        )),
        const SizedBox(height: 0),
        _img_item(false),
        const SizedBox(height: 5),
        SizedBox(
            height: 16,
            child: Label(
              '${widget.signModel.gain.roundDouble(1)}',
              fontsize: 10,
              textColor: !enable
                  ? Color_disEnable
                  : widget.signModel.select
                      ? Colors.white
                      : Colors.black,
              borderColor: !enable
                  ? Color_disEnable
                  : widget.signModel.select
                      ? Color_theme
                      : Colors.black,
              backgroudColor: enable && widget.signModel.select ? Color_theme : Color_bg_view,
            )),
      ],
    );
  }
}
